package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.PermissionService;
import com.itheima.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname RoleController
 * @Description TODO
 * @Date 2020/8/27 8:23 下午
 * @Created by zhaopeng
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;
    @Reference
    private PermissionService permissionService;

    // 分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = roleService.findPage(queryPageBean);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // 添加角色
    @PreAuthorize("hasRole('ROLE_ADMIN')")//权限校验
    @RequestMapping("/add")
    public Result add(@RequestBody Role role){
        try {
            roleService.add(role);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }
    // 根据角色id查询角色
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role role = roleService.findById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,role);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    // 根据角色id查询对应于的权限
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id){
        try {
            List<Integer> ids = roleService.findPermissionIdsByRoleId(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,ids);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //修该权限
    @RequestMapping("/editPermission")
    public Result editPermission(Integer[] permissionIds,Integer roleId){
        try {
            roleService.editPermission(permissionIds,roleId);
            return new Result(true, "修该权限成功");
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false, "修该权限失败");
        }
    }

    //删除角色
    @PreAuthorize("hasRole('ROLE_ADMIN')")//权限校验
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            roleService.delete(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    //查找所有菜单
    @RequestMapping("/findAllMenu")
    public Result findAllMenu() {
        try {
            List<Menu> menu=roleService.findMenuAll();
            ArrayList<Menu> menus = new ArrayList<>();
            for (Menu menu1 : menu) {
                if (menu1.getLevel()==1){
                    menus.add(menu1);
                }
            }
            return new Result(true, "菜单查询成功",menus);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "菜单查询失败");
        }
    }

    //通过角色id查找关联菜单id
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id) {
        try {
            List<Integer> menuId=roleService.findMenuIdsByRoleId(id);
            return new Result(true, "菜单查询id成功",menuId);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "菜单查询id失败");
        }
    }

    //修改菜单
    @RequestMapping("/editMenu")
    public Result editMenu(Integer roleId,@RequestBody List<Integer> listid){
        try {
            roleService.editMenu(listid,roleId);
            return new Result(true, "修改单成功");
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false, "修改菜单失败");
        }
    }
}
